package org.corey.share.db.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.corey.share.db.entity.Article;
import org.corey.share.misc.PageData;

@SuppressWarnings("unchecked")
public class ArticleDaoImpl extends DefaultBaseDao implements ArticleDao {

	public PageData findAll(int pageSize, int pageIndex, String orderBy) {
		int startRecordIndex = PageData.getStartIndex(pageSize, pageIndex);
		Query query = getEM().createQuery(
				"Select a from Article a where a.isDeleted=:isDeleted order by "
						+ orderBy);
		query.setFirstResult(startRecordIndex);
		query.setMaxResults(pageSize);
		List<Article> pageData = query.setParameter("isDeleted", false)
				.getResultList();
		Query countQuery = getEM().createQuery(
				"Select count(a) from Article a where a.isDeleted=:isDeleted");
		countQuery.setParameter("isDeleted", false);
		int totalSize = this.getCount(countQuery);
		PageData<Article> articles = new PageData<Article>(totalSize, pageSize,
				pageIndex, pageData);
		return articles;
	}
	
	public PageData loadArticlesbyCatogory(int pageSize, int pageIndex, String orderBy,Long catotoryId) {
		int startRecordIndex = PageData.getStartIndex(pageSize, pageIndex);
		Query query = getEM().createQuery(
				"Select a from Article a where a.isDeleted=:isDeleted and a.catogory=:catogory order by "
						+ orderBy);
		query.setFirstResult(startRecordIndex);
		query.setMaxResults(pageSize);
		List<Article> pageData = query.setParameter("isDeleted", false).setParameter("catogory", catotoryId)
				.getResultList();
		Query countQuery = getEM().createQuery(
				"Select count(a) from Article a where a.isDeleted=:isDeleted and a.catogory=:catogory");
		countQuery.setParameter("isDeleted", false).setParameter("catogory", catotoryId);
		int totalSize = this.getCount(countQuery);
		PageData<Article> articles = new PageData<Article>(totalSize, pageSize,
				pageIndex, pageData);
		return articles;
	}

	public PageData<Article> loadAllArticles(int pageSize, int pageIndex,
			String orderBy) {
		return (PageData<Article>) findAll(pageSize, pageIndex, orderBy);
	}

	public PageData<Article> loadArticlesWroteBetween(int pageSize,
			int pageIndex, Date start, Date end) {
		int startRecordIndex = PageData.getStartIndex(pageSize, pageIndex);
		Query query = getEM()
				.createQuery(
						"select a from Article a where a.catogory between :start and :end order by a.writeDate desc");
		query.setFirstResult(startRecordIndex);
		query.setMaxResults(pageSize);
		query.setParameter("start", start);
		query.setParameter("end", end);
		List<Article> pageData = query.getResultList();
		Query countQuery = getEM()
				.createQuery(
						"Select count(a) from Article a where a.catogory between :start and :end");
		countQuery.setParameter("start", start);
		countQuery.setParameter("end", end);
		int totalSize = this.getCount(countQuery);
		PageData<Article> articles = new PageData<Article>(totalSize, pageSize,
				pageIndex, pageData);
		return articles;
	}

	public Article loadArticle(Long id) {
		return (Article) load(id, Article.class);
	}

	public boolean removeArticle(Long id) {
		Article removeArticle = loadArticle(id);
		removeArticle.setIsDeleted(true);
		return updateArticle(removeArticle);
	}

	public boolean saveArticle(Article article) {
		return this.save(article);
	}

	public boolean updateArticle(Article article) {
		return this.update(article);
	}

	public int getArticleCountInCatogory(Long catogoryId) {
		Query query = getEM().createQuery(
				"SELECT COUNT(a) FROM Article a where a.catogory=:catogory")
				.setParameter("catogory", catogoryId);
		int count = this.getCount(query);
		return count;
	}

}
